package com.cskaoyan.service.wx_address_service;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.AddressExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.bean.wx_bo.SaveWxAddressBO;
import com.cskaoyan.bean.wx_vo.address.AddressListVO;
import com.cskaoyan.bean.wx_vo.address.DetailWxAddressVO;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.until.CurrentUserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/9 17:25
 */
@Service
public class WxAddressServiceImpl implements WxAddressService{
    @Autowired
    AddressMapper addressMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public AddressListVO list() {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal();
        String username = principal.getUsername();
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(example);
        Integer userId = users.get(0).getId();
        AddressExample example1 = new AddressExample();
        example1.createCriteria().andUserIdEqualTo(userId);
        example1.setOrderByClause("add_time desc");
        List<Address> addresses = addressMapper.selectByExample(example1);
        AddressListVO addressListVO = new AddressListVO(addresses.size(), 1, addresses.size(), 1, addresses);

        return addressListVO;
    }

    @Override
    public DetailWxAddressVO detail(Integer id) {
        Address address = this.addressMapper.selectByPrimaryKey(id);
        // POJO转VO
        DetailWxAddressVO detailWxAddressVO = DetailWxAddressVO.builder()
                .addressDetail(address.getAddressDetail())
                .addTime(address.getAddTime()).areaCode(address.getAreaCode())
                .city(address.getCity()).county(address.getCounty())
                .deleted(address.getDeleted()).id(address.getId())
                .isDefault(address.getIsDefault()).name(address.getName())
                .province(address.getProvince()).tel(address.getTel())
                .updateTime(address.getUpdateTime()).userId(address.getUserId()).build();
        return detailWxAddressVO;
    }

    @Override
    public Integer detail(SaveWxAddressBO saveWxAddressBO) {
        // todo 电话号码的参数校验
        // todo 邮政编码postalCode从哪里获得?前端并没有提供
        // 因为只能有一个默认地址,所以要先看当前提交的地址是不是默认地址,如果是的话需要去修改原来的默认地址
        if (saveWxAddressBO.getIsDefault()) {
            AddressExample addressExample = new AddressExample();
            addressExample.createCriteria().andIsDefaultEqualTo(true);
            Address oldDefaultMarketAddress = Address.builder().isDefault(false)
                    .updateTime(new Date()).build();
            this.addressMapper.updateByExampleSelective(oldDefaultMarketAddress, addressExample);
        }
        // BO转POJO,基础的地址POJO
        Address baseAddress = Address.builder().name(saveWxAddressBO.getName())
                .tel(saveWxAddressBO.getTel()).province(saveWxAddressBO.getProvince())
                .city(saveWxAddressBO.getCity()).county(saveWxAddressBO.getCounty())
                .areaCode(saveWxAddressBO.getAreaCode()).addressDetail(saveWxAddressBO.getAddressDetail())
                .isDefault(saveWxAddressBO.getIsDefault()).build();
        // 传入的id=0表示这是一个新建的地址,需要执行插入操作  主键由数据库自增决定
        // 去映射文件设置useGeneratedKeys="true" 并封装到id属性
        if (saveWxAddressBO.getId() == 0) {
            // 获得当前用户的id
            Integer id = CurrentUserUtil.getId();
            baseAddress.setUserId(id);
            baseAddress.setAddTime(new Date());
            baseAddress.setUpdateTime(new Date());
            this.addressMapper.insertSelective(baseAddress);
            return baseAddress.getId();
        }
        // 传入的id不是0表示修改原有的地址
        baseAddress.setUpdateTime(new Date());
        baseAddress.setId(saveWxAddressBO.getId());
        this.addressMapper.updateByPrimaryKeySelective(baseAddress);
        return baseAddress.getId();
    }

    @Override
    public void delete(Map map) {
        Integer id = (Integer) map.get("id");
        this.addressMapper.deleteByPrimaryKey(id);
    }
}
